#define _CRT_SECURE_NO_WARNINGS

#include "Sort.h"
#include "Stack.h"

void TestInsertSort()
{
	int arr[] = { 3,5,1,8,2,4,15,84,65,98,185,10,66, 41 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	InsertSort(arr, sz);
	PrintArry(arr, sz);
}

void TestShellSort()
{
	int arr[] = { 3,5,1,8,2,4,15,84,65,98,185,10,66,41 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	ShellSort(arr, sz);
	PrintArry(arr, sz);
}

void TestBubbleSort()
{
	int arr[] = { 3,5,1,8,2,4,15,84,65,98,185,10,66,41 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	BubbleSort(arr, sz);
	PrintArry(arr, sz);
}
void TestSelectSort()
{
	int arr[] = { 3,5,1,8,2,4,15,84,65,98,185,10,66,41 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	SelectSort(arr, sz);
	PrintArry(arr, sz);
}
void TestHeapSort()
{
	int arr[] = { 3,5,1,8,2,4,15,84,65,98,185,10,66,41 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	HeapSort(arr, sz);
	PrintArry(arr, sz);
}

void TestGetMidIndex()
{
	int arr1[] = { 1,2,3,4,5 };//2
	int arr2[] = { 1,2,3,9,5,6,7,8 }; //7
	int arr3[] = { 8,2,3,5,5,6,7,60 }; //0
	int arr4[] = { 1,2,9,4,3 };//4

	int sz1 = sizeof(arr1) / sizeof(arr1[0]);
	int sz2 = sizeof(arr2) / sizeof(arr2[0]);
	int sz3 = sizeof(arr3) / sizeof(arr3[0]);
	int sz4 = sizeof(arr4) / sizeof(arr4[0]);

	printf("%d\n", GetMidIndex(arr1, 0, sz1-1));
	printf("%d\n", GetMidIndex(arr2, 0, sz2-1));
	printf("%d\n", GetMidIndex(arr3, 0, sz3-1));
	printf("%d\n", GetMidIndex(arr4, 0, sz4-1));
}

void TestQuickSort()
{
	int arr[] = { 3,5,1,8,2,4,15,84,65,98,185,10,66,41 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	QuickSort(arr, 0, sz);
	PrintArry(arr, sz);
}

void StackTest()
{
	STNode stn;
	STInit(&stn);

	STPush(&stn, 1);
	STPush(&stn, 2);
	STPush(&stn, 3);
	STPush(&stn, 4);
	STPush(&stn, 5);

	while (!STEmpty(&stn))
	{
		printf("%d ", STPop(&stn));
	}

	STDestroy(&stn);
}

void TestQuickSortNoR()
{
	int arr[] = { 3,5,1,8,2,4,15,84,65,98,185,10,66,41 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	QuickSortNoR(arr, 0, sz);
	PrintArry(arr, sz);
}

void TestMergeSort()
{
	int arr[] = { 3,5,1,8,2,4,15,84,65,98,185,10,66,41 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	MergeSort(arr, sz);
	PrintArry(arr, sz);
}

void TestMergeSortNonR()
{
	int arr[] = { 3, 5 ,1 , 8, 2, 4, 15, 84, 65, 98, 185, 10, 66, 41};
	//int arr[] = { 3,5,1,8,2,4,9,6,0,7};
	int sz = sizeof(arr) / sizeof(arr[0]);
	PrintArry(arr, sz);
	MergeSortNonR(arr, sz);
	PrintArry(arr, sz);
}

void TestQuickSortThreeWayDivision()
{
	//int arr[] = { 3, 5 ,1 , 8, 2, 4, 15, 84, 65, 98, 185, 10, 66, 41 };
	int arr[] = { 6, 5 ,6 , 8, 2, 6, 15, 84, 6, 98, 185, 6, 66, 41 };
	//int arr[] = { 3,5,1,8,2,4,9,6,0,7};
	int sz = sizeof(arr) / sizeof(arr[0]);
	PrintArry(arr, sz);
	QuickSortThreeWayDivision(arr, 0, sz);
	PrintArry(arr, sz);
}
void TestCountSort()
{
	//int a[] = { 4,7,1,9,3,6,5,8,3,2,0 };
	//int a[] = { 6,1,2,7,9,3,4,5,10,8 };
	int a[] = { 6, 1, 6, 7, 9, 6, 4, 5, 6, 8 };


	PrintArry(a, sizeof(a) / sizeof(int));
	CountSort(a, sizeof(a) / sizeof(int));

	PrintArry(a, sizeof(a) / sizeof(int));
}
void TestOP()
{
	srand((unsigned int)time(0));
	const int N = 1000000;
	int* a1 = (int*)malloc(sizeof(int) * N);
	int* a2 = (int*)malloc(sizeof(int) * N);
	int* a3 = (int*)malloc(sizeof(int) * N);
	int* a4 = (int*)malloc(sizeof(int) * N);
	int* a5 = (int*)malloc(sizeof(int) * N);
	int* a6 = (int*)malloc(sizeof(int) * N);
	int* a7 = (int*)malloc(sizeof(int) * N);
	int* a8 = (int*)malloc(sizeof(int) * N);

	for (int i = 0; i < N; ++i)
	{
		a1[i] = rand();
		a2[i] = a1[i];
		a3[i] = a1[i];
		a4[i] = a1[i];
		a5[i] = a1[i];
		a6[i] = a1[i];
		a7[i] = a1[i];
		a8[i] = a1[i];
	}

	int begin1 = clock();
	//InsertSort(a1, N);
	int end1 = clock();

	int begin2 = clock();
	ShellSort(a2, N);
	int end2 = clock();

	int begin3 = clock();
	//BubbleSort(a3, N);
	int end3 = clock();

	int begin4 = clock();
	//SelectSort(a4, N);
	int end4 = clock();

	int begin5 = clock();
	HeapSort(a5, N);
	int end5 = clock();

	int begin6 = clock();
	QuickSort(a6, 0, N);
	int end6 = clock();

	int begin7 = clock();
	MergeSort(a7, N);
	int end7 = clock();

	int begin8 = clock();
	CountSort(a8, N);
	int end8 = clock();

	printf("InsertSort:%d\n", end1 - begin1);
	printf("ShellSort:%d\n", end2 - begin2);
	printf("BubbleSort:%d\n", end3 - begin3);
	printf("SelcetSort:%d\n", end4 - begin4);
	printf("HeapSort:%d\n", end5 - begin5);
	printf("QuickSort:%d\n", end6 - begin6);
	printf("MergeSort:%d\n", end7 - begin7);
	printf("CountSort:%d\n", end8 - begin8);

	free(a1);
	free(a2);
	free(a3);
	free(a4);
	free(a5);
	free(a6);
	free(a7);
	free(a8);
}
int main()
{
	//TestInsertSort();
	//TestShellSort();
	//TestBubbleSort();
	//TestSelectSort();
	//TestHeapSort();
	//TestGetMidIndex();
	//StackTest();
	//TestQuickSort();
	//TestQuickSortNoR();
	//TestMergeSort();
	//TestMergeSortNonR();
	//TestQuickSortThreeWayDivision();
	//TestCountSort();
	TestOP();
}